﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;

namespace PatientInformationExtract.Dictionary.Models
{
    class DictionaryTree : INotifyPropertyChanged
    {
        internal static Brush CatalogColor = new SolidColorBrush(Colors.Magenta);
        internal static Brush WordColor = new SolidColorBrush(Colors.Brown);
        internal static Brush POSColor = new SolidColorBrush(Colors.Green);
        internal static Brush FrequencyColor = new SolidColorBrush(Colors.Green);
        internal static Brush SynonymColor = new SolidColorBrush(Colors.Green);
        internal static Brush EnglishColor = new SolidColorBrush(Colors.Green);
        internal static Brush DefaultColor = new SolidColorBrush(Colors.Black);

        private ObservableCollection<DictionaryTree> _dcts;

        public XmlDocument XmlDoc { get { return this._node.OwnerDocument; } }
        private XmlNode _node;
        public XmlNode XmlNode { get { return this._node; } }
        private DictionaryTree _parent;
        public DictionaryTree Parent { get { return this._parent; } }
        private Brush _displayTyepForeground;
        public Brush DisplayTypeForeground { get { return this._displayTyepForeground; } }
        private bool _isAddCatalogEnabled;
        public bool IsAddCatalogEnabled { get { return this._isAddCatalogEnabled; } }
        private bool _isAddWordEnabled;
        public bool IsAddWordEnabled { get { return this._isAddWordEnabled; } }
        private bool _isDeleteEnabled;
        public bool IsDeleteEnabled { get { return this._isDeleteEnabled; } }
        private bool _isRenameEnabled;
        public bool IsRenameEnabled { get { return this._isRenameEnabled; } }
        private bool _isMoveToCatalogEnabled;
        private static List<MenuItem> _itemsMoveToCatalog = new List<MenuItem>();
        public static List<MenuItem> MoveToCatalogMenu { get { return _itemsMoveToCatalog; } }
        public bool IsMoveToCatalogEnabled { get { return this._isMoveToCatalogEnabled; } }
        public bool IsExpanded { get; set; }
        public bool IsSelected { get; set; }

        public ObservableCollection<DictionaryTree> ChildNodes
        {
            get
            {
                if (_dcts == null)
                {
                    try
                    {
                        if (XmlNode.Name != "POS" && XmlNode.Name != "Frequency" && XmlNode.Name != "Synonym" && XmlNode.Name != "English")
                        {
                            XmlNodeList dcts = XmlNode.ChildNodes;
                            if (dcts.Count > 0)
                            {
                                _dcts = new ObservableCollection<DictionaryTree>();
                                foreach (XmlNode node in dcts)
                                    _dcts.Add(new DictionaryTree(this, node));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.StackTrace);
                    }
                }
                return _dcts;
            }
        }

        public string DisplayType
        {
            get
            {
                return XmlNode.Name;
            }
        }

        public string DisplayName
        {
            get
            {

                if (XmlNode.Attributes.Count != 0)
                {
                    return XmlNode.Attributes["Name"].Value;
                }
                else
                {
                    return XmlNode.InnerText;
                }

            }
        }

        public DictionaryTree(DictionaryTree parent, XmlNode node, bool isExpanded, bool isSelected)
        {
            _parent = parent;
            _node = node;
            IsExpanded = isExpanded;
            IsSelected = isSelected;
            switch (node.Name)
            {
                case "Catalog":
                    {
                        _displayTyepForeground = CatalogColor;
                        _isAddCatalogEnabled = true;
                        _isAddWordEnabled = true;
                        _isDeleteEnabled = true;
                        _isRenameEnabled = true;
                        _isMoveToCatalogEnabled = false;
                        break;
                    }
                case "Word":
                    {
                        _displayTyepForeground = WordColor;
                        _isAddCatalogEnabled = false;
                        _isAddWordEnabled = false;
                        _isDeleteEnabled = true;
                        _isRenameEnabled = false;
                        _isMoveToCatalogEnabled = true;
                        break;
                    }
                case "POS":
                    {
                        _displayTyepForeground = POSColor;
                        _isAddCatalogEnabled = false;
                        _isAddWordEnabled = false;
                        _isDeleteEnabled = false;
                        _isRenameEnabled = false;
                        _isMoveToCatalogEnabled = false;
                        break;
                    }
                case "Frequency":
                    {
                        _displayTyepForeground = FrequencyColor;
                        _isAddCatalogEnabled = false;
                        _isAddWordEnabled = false;
                        _isDeleteEnabled = false;
                        _isRenameEnabled = false;
                        _isMoveToCatalogEnabled = false;
                        break;
                    }
                case "Synonym":
                    {
                        _displayTyepForeground = SynonymColor;
                        _isAddCatalogEnabled = false;
                        _isAddWordEnabled = false;
                        _isDeleteEnabled = false;
                        _isRenameEnabled = false;
                        _isMoveToCatalogEnabled = false;
                        break;
                    }
                case "English":
                    {
                        _displayTyepForeground = EnglishColor;
                        _isAddCatalogEnabled = false;
                        _isAddWordEnabled = false;
                        _isDeleteEnabled = false;
                        _isRenameEnabled = false;
                        _isMoveToCatalogEnabled = false;
                        break;
                    }
                case "Dictionary":
                    {
                        _displayTyepForeground = DefaultColor;
                        _isAddCatalogEnabled = true;
                        _isAddWordEnabled = false;
                        _isDeleteEnabled = false;
                        _isRenameEnabled = false;
                        _isMoveToCatalogEnabled = false;
                        break;
                    }
                default:
                    {
                        _displayTyepForeground = DefaultColor;
                        _isAddCatalogEnabled = false;
                        _isAddWordEnabled = false;
                        _isDeleteEnabled = false;
                        _isRenameEnabled = false;
                        _isMoveToCatalogEnabled = false;
                        break;
                    }
            }
        }

        public DictionaryTree(DictionaryTree parent, XmlNode node)
            : this(parent, node, false, false)
        {
        }

        public static ObservableCollection<DictionaryTree> InitRoot(XmlNode rootNode, bool isExpanded, XmlNode selectedNode)
        {
            ObservableCollection<DictionaryTree> dts = new ObservableCollection<DictionaryTree>();
            DictionaryTree dt = new DictionaryTree(null, rootNode, isExpanded, false);
            dts.Add(dt);
            if (selectedNode != null)
            {
                foreach (DictionaryTree child in dt.ChildNodes)
                {
                    if (child.XmlNode == selectedNode)
                    {
                        child.IsSelected = true;
                    }
                }
            }
            return dts;
        }

        public static ObservableCollection<DictionaryTree> InitRoot(XmlNode rootNode, XmlNode selectedNode)
        {
            return InitRoot(rootNode, true, selectedNode);
        }

        public static ObservableCollection<DictionaryTree> InitRoot(XmlNode rootNode)
        {
            return InitRoot(rootNode, true, null);
        }

        public static void SetMoveToCatalogMenu(List<MenuItem> itemCollection)
        {
            _itemsMoveToCatalog = itemCollection;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

    }
}
